import type mapboxgl from 'mapbox-gl'
import { addLayer, registerMapLegend, BaseSymbolLayer, type SymbolStyle } from '@/map'
import type { Airport } from './types'
import { airport2Feature } from './feature'

// 航路点样式
const airportStyle: SymbolStyle = {
  // icon: 'dot-9',
  icon: 'airport-11',
  color: '#C6CDD6',
  textOpacity: 1.0,
  fontScale: 0.8,
  textOffset: [1.5, 0]
}

export let airportLayer: BaseSymbolLayer<Airport>

function initAirportLayer(lst: Airport[]) {
  const opts = {
    id: 'Entity:Airport',
    name: '机场',
    getFeature: (apt: Airport) => airport2Feature(apt, { text: apt.id }),
    style: airportStyle,
    textField: ['get', 'text'] as mapboxgl.Expression,
    minZoom: 4.9
  }
  const layer = new BaseSymbolLayer<Airport>(opts, lst)
  airportLayer = layer
  addLayer(layer)
}

function initAirportLayerLegend() {
  registerMapLegend({
    name: '机场',
    visible() {
      return airportLayer.visible
    },
    setVisible(ok) {
      airportLayer.visible = ok
    }
  })
}

export function initMap(wptLst: Airport[]) {
  initAirportLayer(wptLst)
  initAirportLayerLegend()
}
